package org.example.everyday;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *  https://leetcode-cn.com/problems/the-number-of-weak-characters-in-the-game/
 * @author anlu
 * @date 2022/1/28 11:21
 */
public class NumberOfWeakCharacters {

    public int numberOfWeakCharacters(int[][] ps) {

        int n = ps.length,ans = 0;
        for (int[] arr : ps){
            Arrays.sort(ps,(a,b)->{
                if(a[0] != b[0]){
                    return b[0] - a[0];
                }
                return b[1] - a[1];
            });
        }

        for (int i = 0,max = ps[0][1]; i < n;) {
            int j = i;
            while (j<n && ps[j][0] == ps[i][0]){
                if(i!=0 && ps[j][1]<max){
                    ans++;
                }
                j++;
            }
            max  = Math.max(max,ps[i][1]);
            i = j;
        }

        return ans;
    }

    public int solution1(int[][] properties){
        Arrays.sort(properties,(a,b)->{
            return a[0] == b[0]?(a[1] - b[1]):(b[0] - a[0]);
        });
        int maxDef = 0;
        int ans = 0;
        for (int[] p : properties){
            if(p[1] < maxDef){
                ans++;
            }else{
                maxDef = p[1];
            }
        }
        return ans;

    }

    public static void main(String[] args) {
        NumberOfWeakCharacters n = new NumberOfWeakCharacters();
//        int[][] nums = new int[][]{{2,2},{3,3}};
        int[][] nums = new int[][]{{7,5},{10,4},{4,3},{2,3},{1,6}};
//        int result= n.numberOfWeakCharacters(nums);
        int result = n.solution1(nums);
        System.out.println(result);
    }




}
